package homework.job4

import akka.actor.{Actor, ActorRef}

import scala.beans.BeanProperty

class Slave(name:String,masterRef:ActorRef) extends Actor{

  //是否终止发送心跳
  private var flag= true


  def stop(): Unit ={
    flag=false
  }

  override def receive: Receive = {
    case "start" => timeExc() //启动定时发送心跳
    case "exit" =>{
      println(s"$name 被踢出")
      context.stop(self)
      context.system.terminate()
    }
    case _ => println("slave 获取到master反馈")
  }

  //10s定时发送消息
  def timeExc(): Unit ={
    while(flag){
      masterRef ! "heart"
      Thread.sleep(10* 1000)
    }
  }
}
